%%-*- mode: erlang -*-

{mapping, "exhook.request_failed_action", "emqx_exhook.request_failed_action", [
  {default, "deny"},
  {datatype, {enum, [ignore, deny]}}
]}.

{mapping, "exhook.request_timeout", "emqx_exhook.request_timeout", [
  {default, "5s"},
  {datatype, {duration, ms}}
]}.

{mapping, "exhook.auto_reconnect", "emqx_exhook.auto_reconnect", [
  {default, "60s"},
  {datatype, string}
]}.

{mapping, "exhook.hook_priority", "emqx_exhook.hook_priority", [
  {default, 0},
  {datatype, integer}
]}.

{translation, "emqx_exhook.auto_reconnect", fun(Conf) ->
  case cuttlefish:conf_get("exhook.auto_reconnect", Conf) of
      "false" -> false;
      Dur ->
          case cuttlefish_duration:parse(Dur, ms) of
              Ms when is_integer(Ms) -> Ms;
              {error, Reason} -> error(Reason)
          end
  end
end}.

{mapping, "exhook.pool_size", "emqx_exhook.pool_size", [
  {datatype, integer}
]}.

{mapping, "exhook.server.$name.url", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.cacertfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.certfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.ssl.keyfile", "emqx_exhook.servers", [
  {datatype, string}
]}.

{mapping, "exhook.server.$name.socket_options.keepalive", "emqx_exhook.servers", [
  {default, true},
  {datatype, {enum, [true, false]}}
]}.

{mapping, "exhook.server.$name.socket_options.nodelay", "emqx_exhook.servers", [
  {default, true},
  {datatype, {enum, [true, false]}}
]}.

{mapping, "exhook.server.$name.socket_options.recbuf", "emqx_exhook.servers", [
  {datatype, bytesize}
]}.

{mapping, "exhook.server.$name.socket_options.sndbuf", "emqx_exhook.servers", [
  {datatype, bytesize}
]}.

{translation, "emqx_exhook.servers", fun(Conf) ->
  Filter  = fun(Opts) -> [{K, V} || {K, V} <- Opts, V =/= undefined] end,

  MkSockOpts = fun(Prefix, Conf) ->
                   {socket_options,
                       Filter([{Opt,
                                cuttlefish:conf_get(Prefix ++ ".socket_options." ++ atom_to_list(Opt),
                                                    Conf,
                                                    undefined)}
                                || Opt <- [keepalive, nodelay, recbuf, sndbuf]])}
               end,

  ServerOptions = fun(Prefix) ->
                      case http_uri:parse(cuttlefish:conf_get(Prefix ++ ".url", Conf)) of
                          {ok, {http, _, Host, Port, _, _}} ->
                              [{scheme, http}, {host, Host}, {port, Port},
                               MkSockOpts(Prefix, Conf)
                              ];
                          {ok, {https, _, Host, Port, _, _}} ->
                              [{scheme, https}, {host, Host}, {port, Port},
                               {ssl_options,
                                 Filter([{ssl, true},
                                         {certfile, cuttlefish:conf_get(Prefix ++ ".ssl.certfile", Conf, undefined)},
                                         {keyfile, cuttlefish:conf_get(Prefix ++ ".ssl.keyfile", Conf, undefined)},
                                         {cacertfile, cuttlefish:conf_get(Prefix ++ ".ssl.cacertfile", Conf, undefined)}
                                        ])},
                               MkSockOpts(Prefix, Conf)
                              ];
                          _ -> error(invalid_server_options)
                      end
                  end,

  [{list_to_atom(Name), ServerOptions("exhook.server." ++ Name)}
   || {["exhook", "server", Name, "url"], _} <- cuttlefish_variable:filter_by_prefix("exhook.server", Conf)]
end}.
